﻿

/****** Object:  User Defined Function dbo.FirstDayOfYear    Script Date: 10/14/2009 10:39:06 PM ******/

CREATE FUNCTION FirstDayOfYear (@year int)  
RETURNS int AS  
BEGIN 
--
-- FirstDayOfYear returns day that the year started on as an integer between 1 and 7 where 1 represents Sunday
--
declare @secondTerm int
declare @thirdTerm int
declare @fourthTerm int

set @secondTerm = (@year - 1) / 4 
set @thirdTerm  = (@year - 1) / 100
set @fourthTerm = (@year - 1) / 400

return(  (@year + @secondTerm - @thirdTerm + @fourthTerm) % 7 + 1 )

END

